package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.BalanceListMsgDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.BalanceMsgMapper;
import com.ant.storage.mapper.InventoryMapper;
import com.ant.storage.pojo.BalanceMsg;
import com.ant.storage.pojo.InventoryLog;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.IBalanceMsgService;
import com.ant.storage.service.IInventoryLogService;
import com.ant.storage.utils.commonUtil;
import com.ant.storage.vo.BalanceInventoryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzd
 * @since 2023-10-11
 */
@Service
public class BalanceMsgServiceImpl extends ServiceImpl<BalanceMsgMapper, BalanceMsg> implements IBalanceMsgService {


    @Autowired
    private IBalanceMsgService balanceMsgService;


    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private IInventoryLogService logService;

    @Autowired
    public ServletContext servletContext;

    @Override
    public List<BalanceMsg> queryList(BalanceListMsgDto dto) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        List<BalanceMsg> list =this.baseMapper.selectAllList(dto);
        return list;
    }

    @Override
    public List<String> queryTypeList() {
        return this.baseMapper.queryTypeList();
    }

    @Override
    public ResultVo saveBalanceMsg(Integer wid) {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if (wid == null) {
            throw new AntException(ResultCode.FAILURE.getResultCode(), "仓库id无效");
        }
        List<BalanceInventoryVo> inventoryList =inventoryMapper.BalanceInventoryList(wid);
        if (inventoryList == null && inventoryList.size() == 0) {
            throw new AntException(ResultCode.FAILURE.getResultCode(), "仓库无数据");
        }
        String yearMonth = commonUtil.yearMonth();
        List<BalanceMsg> balanceNumber = balanceMsgService.list(new QueryWrapper<BalanceMsg>()
                .eq("warehouse_id",userInfo.getCityId())
                .eq("balance_number",yearMonth));
        if(balanceNumber==null || balanceNumber.size()==0){
            List<BalanceMsg> list = inventoryList.stream().map(item -> {
                BalanceMsg msg = new BalanceMsg();
                msg.setWarehouseId(item.getWarehouseId());
                msg.setCommodityId(item.getCommodityId());
                msg.setInventoryId(item.getId());
                msg.setBalanceQuantity(item.getGoodsAmount());
                msg.setUnitPrice(item.getGoodsPrice());
                msg.setBalancePrice(item.getGoodsAmount().multiply(item.getGoodsPrice()));
                msg.setBalanceTime(new Date());
                msg.setCreatorBy(userInfo.getNameId().toString());
                msg.setBalanceType(item.getCommodityType());
                msg.setBalanceNumber(commonUtil.yearMonth());
              //msg.setBalanceNumber("202309");
                return msg;
            }).collect(Collectors.toList());
            balanceMsgService.saveBatch(list);
            logService.update(new UpdateWrapper<InventoryLog>().set("balance_msg",yearMonth).isNull("balance_msg"));
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("已结存");
    }


}
